//****************************************************************
//* Copyright (c) 2004 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   MixTempDao.java  $$
//* $$Revision:   1.4  $$
//* $$Author:   lsparaci  $$
//* $$Date:   May 08 2009 10:19:30  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.persistence.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import ar.com.ford.it.fullEconomics.FullEconomicsPrs;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp08Mixtemp;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceConfigurationException;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;
import com.ford.it.persistence.impl.toplink.PersistenceUtility;

public class MixTempDao extends Dao {
	private static final String CLASS_NAME = MixTempDao.class.getName();

	private static final ILogger log = LogFactory.getInstance().getLogger(
			CLASS_NAME);

	/**
	 * Da de alta un registro de Mix Temp
	 * 
	 * @param mixtemp
	 */
	public void createMixtemp(Teesp08Mixtemp teesp08Mixtemp)
			throws PersistenceException {

		String METHOD_NAME = "createMixtemp";
		log.entering(CLASS_NAME, METHOD_NAME);

		Transaction tx = null;

		tx = PersistenceUtil.getTransaction();
		tx.registerInsert(teesp08Mixtemp);
		
		log.exiting(CLASS_NAME, METHOD_NAME);
	}

	/**
	 * Suma los usos de una misma Part, agrupando por catalogo, obteniendo el
	 * total de piezas usadas
	 * 
	 * @return
	 * @throws PersistenceException
	 */
	public Collection<Teesp08Mixtemp> findMixTempSumGroupByPartCatalog()
			throws PersistenceException {

		String METHOD_NAME = "findMixTempSumGroupByPartCatalog";
		log.entering(CLASS_NAME, METHOD_NAME);

		Collection<Teesp08Mixtemp> resultCol = null;
		PersistenceManager pm = null;

		pm = PersistenceUtil.getPersistenceManager();
		Query query = pm.newQuery("findMixTempSumGroupByPartCatalog",
				Teesp08Mixtemp.class);

		resultCol = (Collection) pm.executeQuery(query);

		log.exiting(CLASS_NAME, METHOD_NAME);
		return resultCol;
	}

	/**
	 * Borra la tabla temporal MixTemp
	 * 
	 * @throws FullEconomicsException
	 * @throws PersistenceConfigurationException
	 */
	public void deleteTempTable() throws FullEconomicsException {

		String METHOD_NAME = "deleteTempTable";
		log.entering(CLASS_NAME, METHOD_NAME);

		try {

			String sqlString = " delete from fullee_ddl.TEESP08_MIXTEMP ";
			Transaction tx = PersistenceUtil.newTransaction();
			Map<String, Object> parameters = new HashMap<String, Object>();
			int num = (Integer) PersistenceUtility.modifyData(tx, sqlString,
					parameters);
			System.out.println("Numeros de filas afectadas: " + num);
			PersistenceUtil.commitTransaction();

		} catch (Exception e) {
			log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
					"Delete temp table Exception", e);
			throw new FullEconomicsException(e,
					FullEconomicsPrs.MessageErrors.ProductionVolumesMix.CREATE);
		}

		log.exiting(CLASS_NAME, METHOD_NAME);
	}
	
	 /**
     * 
     * @param ecoV
     * @return
     * @throws PersistenceException
     */
    public Teesp08Mixtemp getMixTempForUpdate(Teesp08Mixtemp mixTemp)
            throws PersistenceException {

        String METHOD_NAME = "getMixTempForUpdate";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesp08Mixtemp result = null;
        Teesp08Mixtemp pk = new Teesp08Mixtemp();
        pk.setEesp08PlantCodeC(mixTemp.getEesp08PlantCodeC());
        pk.setEesp08PartClassCodeC(mixTemp.getEesp08PartClassCodeC());
        pk.setEesp08PartPrefixCodeC(mixTemp.getEesp08PartPrefixCodeC());
        pk.setEesp08PartBaseCodeC(mixTemp.getEesp08PartBaseCodeC());
        pk.setEesp08PartSuffixCodeC(mixTemp.getEesp08PartSuffixCodeC());
        pk.setEesp08VehicleCodeC(mixTemp.getEesp08VehicleCodeC());
        pk.setEesp08CatalModelyearCodeC(mixTemp.getEesp08CatalModelyearCodeC());
        pk.setEesp08CatalSeqCodeC(mixTemp.getEesp08CatalSeqCodeC());
        pk.setEesp08CatalTmaCodeC(mixTemp.getEesp08CatalTmaCodeC());
        pk.setEesp08SfiCodeC(mixTemp.getEesp08SfiCodeC());
        
        result = (Teesp08Mixtemp) super.getObjForUpdate(pk);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }
}
